package com.hk.xmlParse;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.hk.config.MyConfiguration;
import com.hk.configBean.MyMappedStatement;

public class XmlConfigParse {

	private Connection conn = null;
	private Map<String, MyMappedStatement> loadMapper = null;
	public XmlConfigParse(InputStream in1,InputStream in2) {
		conn = XmlConfigParse.build(in1);
		loadMapper = XmlConfigParse.loadMapper(in2);
	}
	public static Connection build(InputStream in) {
		Connection conn = null;
		//InputStream in = this.getClass().getClassLoader().getResourceAsStream(path);
		SAXReader reader = new SAXReader();
		Document doc = null;
		try {
			doc  = reader.read(in);
		} catch (DocumentException e) {
			e.printStackTrace();
			throw new RuntimeException("检查配置文件");
		}
		Element rootElement = doc.getRootElement();
		if(!rootElement.getName().equals("dataSource")) {
			throw new RuntimeException("请检查配置文件的,找不到datasource");
		}
		String driverClassName = null;
		String url = null;
		String username=null;
		String password = null;
		List<Element> properties = rootElement.elements();
		for(Element ele : properties) {
			String name = ele.attributeValue("name");
			String value = getValue(ele);
			if(name==null || value ==null) {
				throw new RuntimeException("请检查配置文件"+"的property"+name+" "+value);
			}
			switch(name) {
				case "driver":
					driverClassName = value;
					break;
				case "url":
					url = value;
					break;
				case "username":
					username= value;
					break;
				case "password":
					password = value;
					break;
			}
		}
		try {
			Class.forName(driverClassName);
			conn = (Connection) DriverManager.getConnection(url, username, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
	private static String getValue(Element ele) {
		return ele.hasContent()?ele.getText():ele.attributeValue("value");
	}
	
	/**
	 * 使用dom4j解析xxxMapper.xml文件。并放入到一个容器中
	 * Map<String,MymappedStatement>   string = namespace+id;保证唯一
	 */
	public static Map<String,MyMappedStatement> loadMapper(InputStream in) {
		Map<String ,MyMappedStatement> map = new HashMap<>();
		//Map<String,MapperBean> map = new HashMap<String, MapperBean>();
		//InputStream in = getClass().getResourceAsStream(mapperXml);
		SAXReader reader = new SAXReader();
		Document doc = null;
		//MapperBean mapperBean = new MapperBean();
		try {
			doc  = reader.read(in);
		} catch (DocumentException e) {
			e.printStackTrace();
			throw new RuntimeException("检查配置文件");
		}
		Element rootElement = doc.getRootElement();
		String namespace = rootElement.attributeValue("namespace");
		//mapperBean.setNamespace(namespace.trim());
		List<MyMappedStatement> sqlMethodList = new ArrayList<MyMappedStatement>(); 
		List<Element> elements = rootElement.elements();
		for(Element ele : elements) {
			MyMappedStatement myMappedStatement = new MyMappedStatement();
			String id = ele.attributeValue("id");
			myMappedStatement.setId(id.trim());
			String sql = ele.getTextTrim();
			myMappedStatement.setSql(sql);
			String parameterType = ele.attributeValue("parameterType");
			String resultType = ele.attributeValue("resultType").trim();
			Object newInstance = null;
			try {
				newInstance = Class.forName(resultType).newInstance();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			myMappedStatement.setResultType(newInstance);
			map.put(namespace+id,myMappedStatement);
		}
		return map;
	}
	public MyConfiguration parse() {
		MyConfiguration myconfiguration = new MyConfiguration();
		myconfiguration.setConn(conn);
		myconfiguration.setMap(loadMapper);
		return myconfiguration;
	}
	
}
